package great.easychat.help;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.Observer;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.bumptech.glide.Glide;
import com.pince.frame.mvvm.FinalVMActivity;
import com.pince.frame.mvvm.architecture.BaseViewModel;
import com.pince.logger.LogUtil;
import com.umeng.analytics.MobclickAgent;

import great.easychat.help.dialog.LoadDialog;
import great.easychat.help.util.ToastUtil;

/**
 * @author athoucai
 * @date 2018/9/7
 */
public abstract class BaseActivity<M extends BaseViewModel> extends FinalVMActivity<M> implements IActivityProxy {
    private ActivityProxyImpl activityProxy = null;

    @Override
    protected boolean checkData(Bundle savedInstanceState) {
        return true;
    }

    @Override
    protected boolean isTitleCenter() {
        return true;
    }

    @Override
    protected Drawable requestToolBarBackground() {
        return ContextCompat.getDrawable(this, requestToolBarBackgroundRes());
    }

    protected int requestToolBarBackgroundRes() {
        return R.drawable.base_shape_toolbar_bg;
    }

    @Override
    protected int requestNavigationIcon() {
        if (false) {
            return R.drawable.nav_dark_arrow_night;
        } else {
            return R.drawable.btn_back_black;
        }
    }

    @Override
    protected void onActivityCreateStart() {
        activityProxy = new ActivityProxyImpl(this);
        super.onActivityCreateStart();
        LogUtil.d("onCreate--> " + BaseActivity.class.getName());
        if (viewModel != null) {
            viewModel.getShowToastLiveData().observe(this, new Observer<String>() {
                @Override
                public void onChanged(@Nullable String msg) {
//                    ToastUtil.show(BaseActivity.this, msg);
                }
            });
            viewModel.getShowLoadingLiveData().observe(this, new Observer<Boolean>() {
                @Override
                public void onChanged(@Nullable Boolean aBoolean) {
                    if (aBoolean) {
                        showLoadDialog(null);
                    } else {
                        hideLoadDialog();
                    }
                }
            });
        }
    }

    @Override
    public void changeTheme() {
        activityProxy.changeTheme();
    }


    // 标题栏是否使用暗黑模式
    protected boolean isSupportDark() {
        return false;
    }

    @Override
    protected void initView(View contentView) {
        initNormalMode();
    }

    //强制横竖屏切换
    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        Window win = getWindow();
        if (win != null) {
            win.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
        }
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }

    @Override
    protected void onStart() {
        super.onStart();
        activityProxy.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        activityProxy.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        activityProxy.onPause();
    }

    @Override
    protected void preDestroy() {
        hideLoadDialog();
    }

    @Override
    protected void onDestroy() {
        activityProxy.onDestroy();
        Glide.with(getApplicationContext()).pauseRequests();
        super.onDestroy();
    }

    public LoadDialog getLoadDialog() {
        return loadDialog;
    }

    private LoadDialog loadDialog = null;

    protected void showLoadDialog(@Nullable String title) {
        if (loadDialog == null) {
            loadDialog = new LoadDialog(this);
        }
        loadDialog.showOnce(title);
    }

    protected void hideLoadDialog() {
        if (loadDialog != null && loadDialog.isShowing()) {
            loadDialog.dismiss();
        }
    }


    @Override
    public <T> LifecycleTransformer<T> bindUntilEvent(@NonNull Lifecycle.Event lifeCycleEvent) {
        return activityProxy.bindUntilEvent(lifeCycleEvent);
    }

    /**
     * 注册eventbus
     */
    @Override
    public void registerEventBus(Object object) {
        activityProxy.registerEventBus(object);
    }

    /**
     * 取消注册eventbus
     */
    @Override
    public void unregisterEventBus(Object object) {
        activityProxy.unregisterEventBus(object);
    }


    private void initNormalMode() {
        setTitleTextColorRes(R.color.color_title_text_normal);
        if (getToolbar() != null) {
            getToolbar().setBackgroundResource(R.color.color_title_back_normal);
        }
    }
}
